//
//  AvatarView.m
//  KKMYForU
//
//  Created by 黄磊 on 13-12-4.
//  Copyright (c) 2013年 黄磊. All rights reserved.
//

#import "AvatarView.h"
#import <QuartzCore/QuartzCore.h>
//#import "UIImageView+AFNetworking.h"

@interface AvatarView ()

@property (nonatomic, strong) UIImageView *imgviewAvatar;
@property (nonatomic, strong) UIButton *btn ;
@end

@implementation AvatarView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        [self settingView];
    }
    return self;
}

- (void)awakeFromNib
{
    [self settingView];
}

- (void)settingView
{
    CGRect rect = self.frame;
    CGFloat width = MIN(rect.size.width, rect.size.height);
    CGRect imgRect = CGRectMake(0, 0, width, width);
    if (!_imgviewAvatar)
    {
        _imgviewAvatar = [[UIImageView alloc] init];
    }
    [_imgviewAvatar setFrame:imgRect];
    [_imgviewAvatar setImage:[UIImage imageNamed:@"img_user"]];
    _merchantPic = @"";
    if (!_btn)
    {
        _btn = [[UIButton alloc] init];
        [_btn setBackgroundColor:[UIColor clearColor]];
        [_btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    [_btn setFrame:self.bounds];

    
    [self setBackgroundColor:[UIColor clearColor]];
    [self addSubview:_imgviewAvatar];
    [self addSubview:_btn];
    if (self.tag == 0) {
        _imgviewAvatar.layer.cornerRadius = width / 2;
        _imgviewAvatar.layer.masksToBounds = YES;
    } else if (self.tag == 2) {
        [self setBackgroundColor:[UIColor whiteColor]];
        rect.size.width = width;
        rect.size.height = width;
        [self setFrame:rect];
        self.layer.cornerRadius = width / 2;
        self.layer.masksToBounds = YES;
        imgRect = CGRectMake(2, 2, width - 4, width - 4);
        [_imgviewAvatar setFrame:imgRect];
        _imgviewAvatar.layer.cornerRadius = (width - 4) / 2;
        _imgviewAvatar.layer.masksToBounds = YES;
    }
    
}
/*
- (void)setMerchantPic:(NSString *)merchantPic
{
    [self setMerchantPic:merchantPic useCache:YES];
}

- (void)setMerchantPic:(NSString *)merchantPic useCache:(BOOL)useCache
{
    if (merchantPic == nil || [merchantPic isEqualToString:@""])
    {
        if (![[NSThread currentThread] isMainThread]) {
            LogError(@"在非主线程中");
        }
        _imgAvatar = [UIImage imageNamed:@"img_mer_default"];
        [_imgviewAvatar setImage:_imgAvatar];
        _merchantPic = @"";
        return;
    }
//    [_imgviewAvatar setImageWithURL:[NSURL URLWithString:[NSString isFullUrl:merchantPic]] placeholderImage:[UIImage imageNamed:@"img_mer_default"]];

//    if (_merchantPic && [_merchantPic isEqualToString:merchantPic])
//    {
//        return;
//    }
    if (_merchantPic.length == 0)
    {
        _imgAvatar = [UIImage imageNamed:@"img_mer_default"];
        [_imgviewAvatar setImage:_imgAvatar];
    }
    _merchantPic = merchantPic;
    NSString *remoteFilePath = [THE_SOURCE_URL stringByAppendingPathComponent:merchantPic];
    if ([merchantPic hasPrefix:@"http:"]) {
        remoteFilePath = [NSString stringWithString: merchantPic];
    }
//    [_imgviewAvatar setImageWithURL:[NSURL URLWithString:remoteFilePath] isNeedCache:useCache placeholderImage: [UIImage imageNamed:@"img_mer_default"]];

    [CacheManager getFileWithUrl:remoteFilePath fileType:eCacheFileAvatar useCache:useCache completion:^(BOOL isSucceed, NSString *message, NSObject *data) {
        if (isSucceed) {
            if (data) {
                _imgAvatar = (UIImage *)data;
                [_imgviewAvatar setImage:_imgAvatar];
            }
        } else {
            _imgAvatar = [UIImage imageNamed:@"img_mer_default"];
            [_imgviewAvatar setImage:_imgAvatar];
        }
    }];
    return;
    
    [[AvatarManager shareInstant] getAvatar:merchantPic useCache:useCache completion:^(BOOL isSucceed, NSString *message, UIImage *image)
     {
         if (isSucceed)
         {
             if (image) {
                 _imgAvatar = image;
                 [_imgviewAvatar setImage:_imgAvatar];
             }
         }
         else
         {
             _imgAvatar = [UIImage imageNamed:@"img_mer_default"];
             [_imgviewAvatar setImage:_imgAvatar];
         }
     }];
}

- (void)setUserPic:(NSString *)userPic useCache:(BOOL)useCache
{
    if (userPic == nil || [userPic isEqualToString:@""])
    {
        if (![[NSThread currentThread] isMainThread]) {
            LogError(@"在非主线程中");
        }
        _imgAvatar = [UIImage imageNamed:@"img_user_default"];
        [_imgviewAvatar setImage:_imgAvatar];
        _merchantPic = @"";
        return;
    }
//    [_imgviewAvatar setImageWithURL:[NSURL URLWithString:[NSString isFullUrl:userPic]] placeholderImage:[UIImage imageNamed:@"img_user_default"]];

//    if (_merchantPic && [_merchantPic isEqualToString:userPic])
//    {
//        return;
//    }
    if (_merchantPic.length == 0) {
        _imgAvatar = [UIImage imageNamed:@"img_user_default"];
        [_imgviewAvatar setImage:_imgAvatar];
    }
    _merchantPic = userPic;
    NSString *remoteFilePath = [THE_SOURCE_URL stringByAppendingPathComponent:userPic];
    if ([userPic hasPrefix:@"http:"]) {
        remoteFilePath = [NSString stringWithString: userPic];
    }
//    [_imgviewAvatar setImageWithURL:[NSURL URLWithString:remoteFilePath] isNeedCache:useCache placeholderImage: [UIImage imageNamed:@"img_user_default"]];
    
    [CacheManager getFileWithUrl:remoteFilePath fileType:eCacheFileAvatar useCache:useCache completion:^(BOOL isSucceed, NSString *message, NSObject *data) {
        if (isSucceed) {
            if (data) {
                _imgAvatar = (UIImage *)data;
                [_imgviewAvatar setImage:_imgAvatar];
            }
        } else {
            _imgAvatar = [UIImage imageNamed:@"img_user_default"];
            [_imgviewAvatar setImage:_imgAvatar];
        }
    }];
    return;
    
    [[AvatarManager shareInstant] getAvatar:userPic useCache:useCache completion:^(BOOL isSucceed, NSString *message, UIImage *image)
     {
         if (isSucceed)
         {
             if (image) {
                 _imgAvatar = image;
                 [_imgviewAvatar setImage:_imgAvatar];
             }
         }
         else
         {
             _imgAvatar = [UIImage imageNamed:@"img_user_default"];
             [_imgviewAvatar setImage:_imgAvatar];
         }
     }];

}

- (void)setImage:(NSString *)imageUrl placeholderImage:(UIImage *)placeholderImage
{
    if (imageUrl == nil || [imageUrl isEqualToString:@""])
    {
        if (![[NSThread currentThread] isMainThread]) {
            LogError(@"在非主线程中");
        }
        [_imgviewAvatar setImage:placeholderImage];
        _merchantPic = @"";
    }
    //    [_imgviewAvatar setImageWithURL:[NSURL URLWithString:[NSString isFullUrl:userPic]] placeholderImage:[UIImage imageNamed:@"img_user_default"]];
    
    //    if (_merchantPic && [_merchantPic isEqualToString:userPic])
    //    {
    //        return;
    //    }
    if (_merchantPic.length == 0) {
        _imgAvatar = placeholderImage;
        [_imgviewAvatar setImage:_imgAvatar];
    }
    _merchantPic = imageUrl;
    NSString *remoteFilePath = [NSString isFullUrl:imageUrl];
    
    [CacheManager getFileWithUrl:remoteFilePath fileType:eCacheFileImage useCache:YES completion:^(BOOL isSucceed, NSString *message, NSObject *data) {
        if (isSucceed) {
            if (data) {
                _imgAvatar = (UIImage *)data;
                [_imgviewAvatar setImage:_imgAvatar];
            }
        } else {
            _imgAvatar = placeholderImage;
            [_imgviewAvatar setImage:_imgAvatar];
        }
    }];
    return;
    
    [_imgviewAvatar setImageWithURL:[NSURL URLWithString:[NSString isFullUrl:imageUrl]] placeholderImage:placeholderImage];
}
*/
- (void)buttonClick
{
    if (_delegate && [_delegate respondsToSelector:@selector(avatarClick)])
    {
        [_delegate avatarClick];
    }
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
